/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bl;

import Config.Localization;
import da.DABranch;
import da.DALoanType;
import entities.Branch;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author foly
 */
public class BLBranch {

    DABranch daBranch;

    public BLBranch() {
        daBranch = new DABranch();
    }

    //get all loan type
    public ArrayList<Branch> getAll() throws Exception {
        return daBranch.getAll();
    }
    
    public ArrayList<Branch> getAllActive() throws Exception {
        return daBranch.getAllActive();
    }
    

    public ArrayList<Branch> getByID(String sid) throws Exception {
        int id = 0;
        boolean check = false;
        try {
            id = Integer.parseInt(sid);
            check = true;
        } catch (Exception ex) {
            check = false;
            //ex.printStackTrace();
        }
        if (check) {
            return daBranch.getByID(id);
        } else {
            JOptionPane.showMessageDialog(null, Localization.getMessage("SearchID"), "Error", JOptionPane.ERROR_MESSAGE);
            return daBranch.getAll();
        }
    }

    
    public ArrayList<Branch> getByName(String name) throws Exception {
        if (name.equals("")) {
            JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("NullError"), "Error", JOptionPane.ERROR_MESSAGE);
            return daBranch.getAll();
        } else {
            return daBranch.getByName(name);
        }
    }
    
    public boolean insert(String name,String address,String phoneNumber,String description , boolean status) throws Exception {
        int result = 0;
        boolean check = true;
        
        String error = "Error : \n"
                + "----------------------------------";

        //check information
        if (name.equals("")) {
            check = false;
            error = error + "\nBranch Name " + Localization.getMessage("Null");
        } 
        
        if (address.equals("")) {
            check = false;
            error = error + "\nAddress" + Localization.getMessage("Null");
        } 
        
        if (phoneNumber.equals("")) {
            check = false;
            error = error + "\nPhone number " + Localization.getMessage("Null");
        } else{
            //check dien thoai
        }
        
        
        if (check) {
            result = daBranch.insert(name, address, phoneNumber, description, status);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("InsertError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public boolean update(String branchID,String name,String address,String phoneNumber,String description , boolean status) throws Exception {
        int result = 0;
        int id = 0;
        
        boolean check = true;

        try{
            id = Integer.parseInt(branchID);
        }
        catch(Exception ex){
        
        }
        String error = "Error : \n"
                + "----------------------------------";
         //check information
        if (name.equals("")) {
            check = false;
            error = error + "\nBranch Name " + Localization.getMessage("Null");
        } 
        
        if (address.equals("")) {
            check = false;
            error = error + "\nAddress" + Localization.getMessage("Null");
        } 
        
        if (phoneNumber.equals("")) {
            check = false;
            error = error + "\nPhone number " + Localization.getMessage("Null");
        } else{
            //check dien thoai
        }


        if (check) {
            Branch branch = new Branch(id, name, address,phoneNumber ,description, status);
            result = daBranch.update(branch);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("UpdateError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public void disable(String branchID) throws Exception {
        int result = 0;
        int id = 0;
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";
        try {
            id = Integer.parseInt(branchID);
        } catch (Exception ex) {
            check = false;
            error = error + "\n" + Localization.getMessage("SearchID");
            //ex.printStackTrace();
        }

        if (check) {
            result = daBranch.Disable(id);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("delelteError"), "Error", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public void enable(String branchID) throws Exception {
        int result = 0;
        int id = 0;
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";
        try {
            id = Integer.parseInt(branchID);
        } catch (Exception ex) {
            check = false;
            error = error + "\n" + Localization.getMessage("SearchID");
            //ex.printStackTrace();
        }

        if (check) {
            result = daBranch.Enable(id);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("delelteError"), "Error", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
